package com.jinlejia.bbs.logic.message;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.bbs.bean.CommentBean;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.InformationBean;
import com.jinlejia.bbs.bean.base.SoundBean;
import com.jinlejia.bbs.http.entity.MessageRemindEntity;
import com.jinlejia.bbs.http.entity.base.PageEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;
import com.jinlejia.bbs.http.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;

/**
 * @author 王进 on 2019/3/27.
 */

public class MessageAnswerLogic {
    private final Context context;
    private ArrayList<CommentBean> dataList;
    private final MessageLikeListLogic.LogicCallBack callBack;

    public MessageAnswerLogic(Context context,ArrayList<CommentBean> dataList,MessageLikeListLogic.LogicCallBack callBack){
        this.context = context;
        this.dataList = dataList;
        this.callBack = callBack;
    }

    private int pageIndex;
    public void refresh(){
        pageIndex = 1;
        loadData(pageIndex);
    }

    public void load(){
        pageIndex = pageIndex+1;
        loadData(pageIndex);
    }

    private void loadData(final int pageIndex){
        JSONObject json = new JSONObject();
        json.put("resourceType",1);
        json.put("pageIndex",pageIndex);
        json.put("pageSize",20);
        httpCallForm(BBSConfig.listRemind,json).execute(httpCallBack(context, new HttpCallBack() {
            @Override
            public void onError(Exception e) {
                if (callBack==null)return;
                callBack.onResult(false);
            }

            @Override
            public void onResponse(HttpUtils.JsonParse parse) {
                if (callBack==null)return;
                TypeReference<PageEntity<MessageRemindEntity>> reference = new TypeReference<PageEntity<MessageRemindEntity>>() {};
                PageEntity<MessageRemindEntity> response = parse.parsePageEntity(reference);
                List<MessageRemindEntity> data = response.getData();
                List<CommentBean> list = new ArrayList<>();
                for (MessageRemindEntity entity: data) {
                    CommentBean bean = new CommentBean();
                    //1.设置评论者
                    BBSUserBean commentUser = new BBSUserBean();
                    commentUser.setUserPin(entity.getTouchPin());
                    commentUser.setUserName(entity.getTouchName());
                    commentUser.setUserAddress(entity.getStoreName());
                    commentUser.setUserAvatar(BBSConfig.headerUrl(entity.getImageUrl()));
                    bean.setCommentUser(commentUser);
                    //2. 设置评论内容
                    InformationBean commentInfo = new InformationBean();
                    commentInfo.setMessageContent(entity.getCommentContent());
                    bean.setCommentInfo(commentInfo);

                    //3.设置被评论者
                    BBSUserBean commentedUser = new BBSUserBean();
                    commentedUser.setUserPin(entity.getPin());
                    commentedUser.setUserName(entity.getName());

                    bean.setCommentedUser(commentedUser);

                    //4.设置被评论内容
                    InformationBean commentedInfo = new InformationBean();

                    if (entity.getContentType()==1){
                        commentedInfo.setMessageContent(entity.getContent());
                    }else if (entity.getContentType()==2){
                        commentedInfo.setSoundContent(SoundBean.create(entity.getContent()));
                    }else {
                        if (!TextUtils.isEmpty(entity.getContent())){
                            commentedInfo.setMessageContent(entity.getContent());
                        }
                    }
                    bean.setCommentedInfo(commentedInfo);
                    //5.设置评论列表方式
                    bean.setCommentAction(CommentBean.ACTION_REPLY);
                    bean.setTopicCode(entity.getResourceCode());
                    list.add(bean);
                }
                if (pageIndex==1){
                    dataList.clear();
                }
                dataList.addAll(list);
                callBack.onResult(response.getNextPageAvailable());
            }

            @Override
            public void onNoResponse(int code) {
                if (callBack==null)return;
                callBack.onResult(false);
            }
        }));
    }

    public interface LogicCallBack{
        void onResult(boolean hasNextPage);
    }
}
